package conf

import (
	"gopkg.in/ini.v1"
	"strings"
)

var (
	HttpPort string

	Host        string
	ArticlePath string
	AvatarPath  string

	Db          string
	DbHost      string
	DbPort      string
	DbUser      string
	DbPassword  string
	DbName      string
	MQPath      string
	RedisDb     string
	RedisAddr   string
	RedisPw     string
	RedisDbName string

	MongoDBName string
	MongoDBAddr string
	MongoDBUser string
	MongoDBPwd  string
	MongoPort   string
)

func Init() {

	//本地读取环境变量

	file, err := ini.Load("./conf/config.ini")
	if err != nil {
		panic(err)
	}
	LoadServer(file)
	LoadMysql(file)
	LoadRedis(file)
	LoadMongo(file)
	LoadPhotoPath(file)

	//mysql
	MQPath = strings.Join([]string{DbUser, ":", DbPassword, "@tcp(", DbHost, ":", DbPort, ")/", DbName, "?charset=utf8mb4&parseTime=true"}, "")

}

func LoadServer(file *ini.File) {

	HttpPort = file.Section("service").Key("HttpPort").String()
}
func LoadPhotoPath(file *ini.File) {
	Host = file.Section("path").Key("Host").String()
	ArticlePath = file.Section("path").Key("ArticlePath").String()
	AvatarPath = file.Section("path").Key("AvatarPath").String()

}
func LoadMysql(file *ini.File) {
	Db = file.Section("mysql").Key("DB").String()
	DbHost = file.Section("mysql").Key("DbHost").String()
	DbPort = file.Section("mysql").Key("DbPort").String()
	DbUser = file.Section("mysql").Key("DbUser").String()
	DbPassword = file.Section("mysql").Key("DbPassword").String()
	DbName = file.Section("mysql").Key("DbName").String()

}
func LoadRedis(file *ini.File) {
	RedisDb = file.Section("redis").Key("RedisDbName").String()
	RedisAddr = file.Section("redis").Key("RedisDbName").String()
	RedisPw = file.Section("redis").Key("RedisDbName").String()
	RedisDbName = file.Section("redis").Key("RedisDbName").String()
}
func LoadMongo(file *ini.File) {

	MongoDBName = file.Section("mongoDB").Key("MongoDBName").String()
	MongoDBAddr = file.Section("mongoDB").Key("MongoDBAddr").String()
	MongoDBUser = file.Section("mongoDB").Key("MongoDBUser").String()
	MongoDBPwd = file.Section("mongoDB").Key("MongoDBPwd").String()
	MongoPort = file.Section("mongoDB").Key("MongoPort").String()
}
